vim.g.mapleader = " "
local keymap = vim.keymap

--- 插入模式---
keymap.set("i","jk", "<ESC>")


--- 视觉模式---
--单行移动
keymap.set("v", "J", ":m '>+1<CR>gv=gv")
keymap.set("v", "K", ":m '<-2<CR>gv=gv")

--- 正常模式---
-- 窗口
keymap.set("n","<leader>s", ":w<CR>") --  save
keymap.set("n","<leader>q", ":q<CR>") --  quit
keymap.set("n","<C-q>", ":q<CR>") --  退出
keymap.set("n","<leader>q", ":q<CR>") --  退出
keymap.set("n", "<leader>sv", "<C-w>v<CR>") -- 水平新增窗口
keymap.set("n", "<leader>sh", "<C-w>s<CR>") -- 垂直新增窗口


-- 取消高亮
keymap.set("n","<leader>nh", ":nohl<CR>")



-- 插件---
-- nvim-tree
keymap.set("n", "<leader>e", ":NvimTreeToggle<CR>")


-- 切换buffer
keymap.set("n", "<Tab>", ":bnext<CR>")
keymap.set("n", "<S-Tab>", ":bprevious<CR>")

--lsp
keymap.set('n', '[d', vim.diagnostic.goto_prev)
keymap.set('n', ']d', vim.diagnostic.goto_next)


vim.api.nvim_create_autocmd('LspAttach', {
  group = vim.api.nvim_create_augroup('UserLspConfig', {}),
  callback = function(ev)
    -- Enable completion triggered by <c-x><c-o>
    vim.bo[ev.buf].omnifunc = 'v:lua.vim.lsp.omnifunc'
    local opts = { buffer = ev.buf }
    vim.keymap.set('n', 'gD', vim.lsp.buf.declaration, opts)
    vim.keymap.set('n', 'gd', vim.lsp.buf.definition, opts)
    vim.keymap.set('n', 'K', vim.lsp.buf.hover, opts)
    vim.keymap.set({ 'n', 'v' }, '<space>ca', vim.lsp.buf.code_action, opts)
  end,
})
